﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using BLL;
using DTO;

namespace GaraOto
{
    public partial class frmCardManager : DevExpress.XtraEditors.XtraUserControl
    {
        TypeBLL typeBLL = new TypeBLL();
        CardBLL cardBLL = new CardBLL();
        public frmCardManager()
        {
            InitializeComponent();            
            BindingDataType();
            FillDataComboboxType();
            BindingDataCard();
        }

        public void BindingDataType()
        {
            BindingSource bs = new BindingSource();
            bs.DataSource = typeBLL.SelectAll();
            bnvTypes.BindingSource = bs;
            gcTypes.DataSource = bs;

            btnEditType.Enabled = false;
        }

        public void BindingDataCard()
        {
            BindingSource bs = new BindingSource();
            bs.DataSource = cardBLL.SelectAll();
            bnvCards.BindingSource = bs;
            gcCards.DataSource = bs;

            btnEditCard.Enabled = false;
        }

        public void FillDataComboboxType()
        {
            List<TypeDTO> list = typeBLL.FillDataCombobox();
            cbeCardType.Properties.Items.Clear();
            for (int i = 0; i < list.Count; i++)
            {
                cbeCardType.Properties.Items.Add(list[i].TypeName);
            }
            cbeCardType.SelectedIndex = 0;

        }

        public int getValueCombobox(string typeName)
        {
            List<TypeDTO> list = typeBLL.FillDataCombobox();
            for (int i = 0; i < list.Count; i++)
            {
                if (list[i].TypeName.Equals(typeName))
                {
                    return list[i].TypeID;
                }
            }
            return -1;
        }

        private void ResetType()
        {
            teTypeName.Text = "";
            teFeeRegister.Text = "";
            teFeeExtension.Text = "";
            cbeStatusType.SelectedIndex = 0;

            btnAddType.Enabled = true;
            btnEditType.Enabled = false;
        }

        private void ResetCard()
        {
            teCardID.Text = "";
            cbeCardType.SelectedIndex = 0;
            cbeCardStatus.SelectedIndex = 0;

            teCardID.Enabled = true;
            btnAddCard.Enabled = true;
            btnEditCard.Enabled = false;
        }

        private void btnAddType_Click(object sender, EventArgs e)
        {
            TypeDTO typeDTO = new TypeDTO();
            typeDTO.TypeName = teTypeName.Text;
            typeDTO.FeeRegister = teFeeRegister.Text;
            typeDTO.FeeExtension = teFeeExtension.Text;
            typeDTO.Status = cbeStatusType.SelectedItem.ToString();

            string message = typeBLL.Insert(typeDTO);
            if (message.Equals(MessageAction.Success))
            {
                MessageBox.Show("Thêm mới loại thẻ thành công");
                BindingDataType();
                ResetType();
                FillDataComboboxType();
            }
            else
            {
                MessageBox.Show(message);
            }
        }

        private void btnEditType_Click(object sender, EventArgs e)
        {
            TypeDTO typeDTO = new TypeDTO();
            typeDTO.TypeID = Convert.ToInt32(gvTypes.GetRowCellValue(gvTypes.GetSelectedRows()[0],gcTypeID).ToString());
            typeDTO.TypeName = teTypeName.Text;
            typeDTO.FeeRegister = teFeeRegister.Text;
            typeDTO.Status = cbeStatusType.SelectedItem.ToString();

            string message = typeBLL.Update(typeDTO);
            if (message.Equals(MessageAction.Success))
            {
                MessageBox.Show("Cập nhật loại thẻ thành công");
                BindingDataType();
                ResetType();
                FillDataComboboxType();
            }
            else
            {
                MessageBox.Show(message);
            }
        }

        private void gvTypes_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            if (gvTypes.GetRowCellValue(gvTypes.GetSelectedRows()[0], gcTypeName) != null)
            {
                teTypeName.Text = gvTypes.GetRowCellValue(gvTypes.GetSelectedRows()[0], gcTypeName).ToString();
            }
            if (gvTypes.GetRowCellValue(gvTypes.GetSelectedRows()[0], gcFeeRegister) != null)
                teFeeRegister.Text = gvTypes.GetRowCellValue(gvTypes.GetSelectedRows()[0], gcFeeRegister).ToString();
            if (gvTypes.GetRowCellValue(gvTypes.GetSelectedRows()[0], gcFeeExtension) != null)
                teFeeExtension.Text = gvTypes.GetRowCellValue(gvTypes.GetSelectedRows()[0], gcFeeExtension).ToString();
            cbeStatusType.SelectedItem = gvTypes.GetRowCellValue(gvTypes.GetSelectedRows()[0], gcStatusType).ToString();

            btnAddType.Enabled = false;
            btnEditType.Enabled = true;
        }

        private void btnResetType_Click(object sender, EventArgs e)
        {
            ResetType();
        }

        private void gvCards_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            teCardID.Text = gvCards.GetRowCellValue(gvCards.GetSelectedRows()[0],gcCardID).ToString();
            cbeCardType.SelectedItem = gvCards.GetRowCellValue(gvCards.GetSelectedRows()[0], gcTypeName).ToString();
            cbeCardStatus.SelectedItem = gvCards.GetRowCellValue(gvCards.GetSelectedRows()[0], gcStatus).ToString();

            teCardID.Enabled = false;
            btnAddCard.Enabled = false;
            btnEditCard.Enabled = true;
        }

        private void btnAddCard_Click(object sender, EventArgs e)
        {
            CardDTO cardDTO = new CardDTO();
            if (!cardBLL.CheckCardIDExist(teCardID.Text.Trim()))
            {
                cardDTO.CardID = teCardID.Text.Trim();
                cardDTO.TypeID = getValueCombobox(cbeCardType.SelectedItem.ToString());
                cardDTO.Status = cbeCardStatus.SelectedItem.ToString();

                string message = cardBLL.Insert(cardDTO);
                if (message.Equals(MessageAction.Success))
                {
                    MessageBox.Show("Thêm mới thẻ thành công");
                    BindingDataCard();
                    ResetCard();
                }
                else
                {
                    MessageBox.Show(message);
                }
            }
            else
            {
                MessageBox.Show("ID thẻ đã tồn tại");
            }
        }

        private void btnResetCard_Click(object sender, EventArgs e)
        {
            ResetCard();
        }

        private void btnEditCard_Click(object sender, EventArgs e)
        {
            CardDTO cardDTO = new CardDTO();
            cardDTO.CardID = teCardID.Text;
            cardDTO.TypeID = getValueCombobox(cbeCardType.SelectedItem.ToString());
            cardDTO.Status = cbeCardStatus.SelectedItem.ToString();

            string message = cardBLL.Update(cardDTO);
            if (message.Equals(MessageAction.Success))
            {
                MessageBox.Show("Cập nhật thông tin thẻ thành công");
                BindingDataCard();
                ResetCard();
            }
            else
            {
                MessageBox.Show(message);
            }
        }

        private void frmCardManager_Load(object sender, EventArgs e)
        {
            teCardID.Focus();
        }

    }
}
